package com.owc.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.owc.common.enums.CallbackTypeEnum;
import com.owc.common.tool.BeanUtils;
import com.owc.dao.DrawFlowMapper;
import com.owc.dao.TransferRecordMapper;
import com.owc.domain.BaseResult;
import com.owc.domain.dto.admin.ChainBean;
import com.owc.domain.dto.admin.DrawDTO;
import com.owc.domain.dto.admin.DrawDetailDTO;
import com.owc.domain.entity.DrawFlow;
import com.owc.domain.entity.TransferRecord;
import com.owc.domain.vo.admin.DrawQueryVo;
import com.owc.service.admin.DrawService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2019/12/24 15:03
 * @Version 1.0.0
 * @Description <>
 **/
@Service
public class DrawServiceImpl implements DrawService {

    @Resource
    private DrawFlowMapper drawFlowMapper;
    @Resource
    private TransferRecordMapper transferRecordMapper;

    @Override
    public BaseResult<List<DrawDTO>> list(DrawQueryVo drawQueryVo) {
        PageHelper.startPage(drawQueryVo.getPageIndex(),drawQueryVo.getPageSize());
        List<DrawDTO> drawFlows = drawFlowMapper.selectByQueryModel(drawQueryVo);
        PageInfo<DrawDTO> pageInfo = new PageInfo<>(drawFlows);
        drawFlows.forEach(drawDTO -> drawDTO.setType(CallbackTypeEnum.DRAW_IN_TYPE.getType().equals(drawDTO.getTypeStr() + "")? 1 : 2));
        return BaseResult.success(drawFlows,pageInfo.getTotal());
    }

    @Override
    public DrawDetailDTO detail(Long id) {
        DrawDetailDTO detailDTO = new DrawDetailDTO();

        //获取划转基本信息
        DrawFlow drawFlow = drawFlowMapper.selectByPrimaryKey(id);
        detailDTO.setDetail(new DrawDetailDTO.DetailBean().convertFrom(drawFlow));

        //获取区块链信息
        List<TransferRecord> transferRecords = transferRecordMapper.selectByFlowId(drawFlow.getDrawoutId());
        if(transferRecords != null && !transferRecords.isEmpty()){
            detailDTO.setChain(new ChainBean().convertFrom(transferRecords.get(0)));
        }else{
            detailDTO.setChain(new ChainBean());
        }
        //流水信息
        detailDTO.setFlowList(new ArrayList<>());
        return detailDTO;
    }


}
